from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait  # 用于实例化一个Driver的显式等待
from selenium.webdriver.common.by import By  # 内置定位器策略集
# 内置预期条件函数，具体API请参考此小节后API链接
from selenium.webdriver.support import expected_conditions as EC
import time


driver = webdriver.Chrome()
driver.get('https://www.amazon.com/Best-Sellers-Health-Personal-Care/zgbs/hpc/ref=zg_bs_nav_0')
time.sleep(45)
# 存储
goodsArr = []
goods = {
  'url': '',
  'name': '',
  'img': '',
  'star': '',
  'reviews': '',
  'price': '',
  'isThousand': ''
}
print(8)
try:
    DliArr = driver.find_elements_by_css_selector('#zg-ordered-list .zg-item-immersion')
    for index, ele in enumerate(DliArr):
      
      url = driver.find_elements_by_css_selector('#zg-ordered-list .zg-item-immersion .a-link-normal')[index].get_attribute('href')
      name = driver.find_elements_by_css_selector('#zg-ordered-list .zg-item-immersion .p13n-sc-truncated')[index].text
      img = driver.find_elements_by_css_selector('#zg-ordered-list .zg-item-immersion .a-section.a-spacing-small img')[index].get_attribute('src')
      a = driver.find_elements_by_css_selector('#zg-ordered-list .zg-item-immersion .aok-inline-block.zg-item .a-icon-row.a-spacing-none .a-icon-alt')
      b = driver.find_elements_by_css_selector('#zg-ordered-list .zg-item-immersion .aok-inline-block.zg-item .a-icon-row.a-spacing-none .a-icon-alt')[0]
      star = driver.find_elements_by_css_selector('#zg-ordered-list .zg-item-immersion .aok-inline-block.zg-item .a-icon-row.a-spacing-none .a-icon-alt')[index].text
      # star = driver.find_elements_by_xpath('//*[@id="zg-ordered-list"]/li[19]/span/div/span/div[1]/a[1]/i/span').text
      reviews = driver.find_elements_by_css_selector('#zg-ordered-list .zg-item-immersion .aok-inline-block.zg-item .a-icon-row.a-spacing-none .a-size-small.a-link-normal')[index].text
      price = driver.find_elements_by_css_selector('#zg-ordered-list .zg-item-immersion .aok-inline-block.zg-item .p13n-sc-price')[index].text
      iThousand = 'false'
      print("  ")
      print("  ")
      print('链接      '+url) 
      print('名称      ' + name)
      print(img)
      print('星星      ' + star)
      print('reviews  ' + reviews)
      print('价格      '+price)

    # 跳往下一个页面
    # driver.find_elements_by_css_selector('#zg-center-div .a-pagination .a-last a').get_attribute("href")



finally:
    driver.close()  # close the driver



